#include<iostream>
#include<string>
#include<time.h>
#include"five-in-a-row.h"

 Board::Board():size(10)
 {
    board = new char *[size];
    for (int i = 0; i < size;i++)
    {
       board[i] = new char[size];
    }
    for (int i = 0; i < size;i++)
    {
       for (int j = 0; j < size;j++)
       {
          board[i][j] = '-';
       }
    }
 }

 void Board::PrintRow()
 {
       std::cout<<"      ";
    for (int i = 0; i < size; i++)
    {
       std::cout << i+1 << "    ";
    }
    std::cout << std::endl;
    for (int i = 0; i < size; i++)
    {
          std::cout << i + 1 << "   ";
       for (int j = 0; j < size;j++)
       {
         if(i>=9&&j==0)
         {
            std::cout << " " << board[i][j] << "  ";
            continue;
         }
         std::cout << "  " << board[i][j] << "  ";
       }
       std::cout << std::endl;
    }
 }
bool Player::MakeMove(Board &a, int row, int column, char player)
{
    if (row < 0 || row >= a.size || column < 0 || column >= a.size)
    {
        std::cout<<"非法下棋位置，请重新输入。\n";
        return false;
    }
    if (a.board[row][column] != '-')
    {
        std::cout<<"该位置已经有棋子了，请重新输入。\n";
        return false;
    }
    a.board[row][column] = player;
    return true;
}
bool IsValidPlace(Board &a,int row, int column)
{
    if (row < 0 || row >= a.size || column < 0 || column >= a.size)
    {
        return false;
    }
    return true;
}
bool Player::CheckIsWin(Board &a, int row, int column, char player)
{
   int direction[4][2] =
       {
           {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
   // 遍历四个方向数棋子数量
   for (int i = 0; i < 4; i++)
   {
      int count = 1;
      // 下一颗棋子的位置
      int dx = row + direction[i][0];
      int dy = column + direction[i][1];
      while (IsValidPlace(a,dx, dy) && a.board[dx][dy] == player)
      {
         count++;
         if (count >= 5)
         {
            return true;
         }
         dx = dx + direction[i][0];
         dy = dy + direction[i][1];
      }
      dx = row - direction[i][0];
      dy = column - direction[i][1];
      while (IsValidPlace(a,dx, dy) && a.board[dx][dy] == player)
      {
         count++;
         if (count >= 5)
         {
            return true;
         }
         dx = dx - direction[i][0];
         dy = dy - direction[i][1];
      }
    }
    return false;
}
bool Player::CheckIsDraw(Board &a, char player)
{
    for (int i = 0; i < a.size; i++)
    {
        for (int j = 0; j < a.size; j++)
        {
            if (a.board[i][j] == '-')
            {
                return false;
            }
        }
    }
    return true;
}
bool Player::AIMakeMove(Board &a, char player, char ai, int PlayerRow, int PlayerColumn)
{
    // 优先取胜策略
    for (int i = 0; i < a.size; i++)
    {
        for (int j = 0; j < a.size; j++)
        {
            if (a.board[i][j] == '-')
            {
                a.board[i][j] = ai;
                if (CheckIsWin(a, i, j, ai) == true)
                {
                    return true;
                }
                a.board[i][j] = '-';
            }
        }
    }
    // 阻止对手策略
    for (int i = 0; i < a.size; i++)
    {
        for (int j = 0; j < a.size; j++)
        {
            if (a.board[i][j] == '-')
            {
                a.board[i][j] = player;
                if (CheckIsWin(a, i, j, ai) == true)
                {
                    a.board[i][j] = ai;
                    return true;
                }
                a.board[i][j] = '-';
            }
        }
    }
    int direction[8][2] =
        {
            {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
    // 随机数的产生由随机种子决定，随机种子不变，随机数就不会变
    // 以当前时间作为随机种子

    while (1)
    {
        srand(time(NULL));
        int i = rand() % 8;
        int dx = PlayerRow + direction[i][0];
        int dy = PlayerColumn + direction[i][1];
        if (IsValidPlace(a,dx, dy) && a.board[dx][dy] == '-')
        {
            a.board[dx][dy] = ai;
            return true;
        }
    }
    return false;
}